package senpec.qc.harmony.base.utils;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import senpec.qc.harmony.base.exception.NoResultException;
import senpec.qc.harmony.base.mybatisplus.Result;
import senpec.qc.harmony.system.user.UserMapper;

@RestControllerAdvice
@Slf4j
@RequiredArgsConstructor
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    public ResponseEntity<Result<?>> handleException(Exception e) {
        if (e instanceof BadCredentialsException) {
            return ResponseEntity.ok(Result.error().msg("密码不正确"));
        } else if (e instanceof NoResultException) {
            return ResponseEntity.ok(Result.error().msg(e.getMessage()));
        }
        return ResponseEntity.ok(Result.error().msg(e.getMessage()));
    }
}